package by.dkusch.aircompany.webapp.page.regional.panel;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.PropertyModel;

import by.dkusch.aircompany.model.filter.FilterAirport;
import by.dkusch.aircompany.model.tables.Airport;
import by.dkusch.aircompany.model.tables.Airport_;
import by.dkusch.aircompany.model.tables.Region_;
import by.dkusch.aircompany.services.AirportService;
import by.dkusch.aircompany.webapp.page.base.BaseLayout;
import by.dkusch.aircompany.webapp.page.base.common.AddNewItemPanel;
import by.dkusch.aircompany.webapp.page.base.common.CommonActionPanel;
import by.dkusch.aircompany.webapp.page.base.common.EntityDataProviderFilter;
import by.dkusch.aircompany.webapp.page.base.common.SelectorActionPanel;
import by.dkusch.aircompany.webapp.page.base.enums.SelectorType;
import by.dkusch.aircompany.webapp.page.base.panels.MenuPanel;
import by.dkusch.aircompany.webapp.page.regional.AirportPage;

public class AirportListPanel extends Panel {

	private static final long serialVersionUID = 1L;
	@Inject
	private AirportService airportService;
	private EntityDataProviderFilter<Long, Airport, FilterAirport> airportDataProvider;

	public AirportListPanel(String id, int itemsPerPage, final boolean choiseFl) {
		super(id);

		airportDataProvider = new EntityDataProviderFilter<Long, Airport, FilterAirport>(airportService);
		airportDataProvider.setSort(Airport_.code, SortOrder.ASCENDING);
		airportDataProvider.setFilterState(new FilterAirport());

		setOutputMarkupId(true);

		DataView<Airport> dataView = new DataView<Airport>("airportList", airportDataProvider, itemsPerPage) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(Item<Airport> item) {
				final Airport airport = item.getModelObject();
				item.add(new CheckBox("arcFl").setEnabled(false));
				item.add(new Label("code"));
				item.add(new Label("type"));
				item.add(new Label("name"));
				item.add(new Label("continent"));
				item.add(new Label("region.code"));
				item.add(new Label("country.name"));
				if (choiseFl) {
					item.add(new SelectorActionPanel<Long>("action-panel", airport.getId(), SelectorType.AIRPORT));
				} else {
					item.add(new CommonActionPanel<Long, Airport, AirportPage>("action-panel", airportService, airport, AirportPage.class, AirportListPanel.this).setVisible(false));
				}

			}
		};


		FilterForm<FilterAirport> filterForm = new FilterForm<FilterAirport>("filterForm", airportDataProvider);
		filterForm.add(new TextField<String>("filter-code", PropertyModel.<String> of(airportDataProvider, "filterState.code")));
		filterForm.add(new TextField<String>("filter-name", PropertyModel.<String> of(airportDataProvider, "filterState.name")));
		filterForm.add(new TextField<String>("filter-type", PropertyModel.<String> of(airportDataProvider, "filterState.type")));
		filterForm.add(new TextField<String>("filter-region", PropertyModel.<String> of(airportDataProvider, "filterState.regionCode")));
		filterForm.add(new TextField<String>("filter-continent", PropertyModel.<String> of(airportDataProvider, "filterState.continent")));
		filterForm.add(new TextField<String>("filter-country", PropertyModel.<String> of(airportDataProvider, "filterState.countryCode")));

		final WebMarkupContainer divTable = new WebMarkupContainer("div-table");
		final PagingNavigator pagingNavigator = new PagingNavigator("paging", dataView);
		divTable.setOutputMarkupId(true);
		pagingNavigator.setOutputMarkupId(true);

		filterForm.add(new AjaxButton("airport-search", filterForm) {
			/**
				 * 
				 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so that it is hidden
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
				target.add(pagingNavigator);
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so errors are shown
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
			}
		});


		divTable.add(dataView);
		divTable.add(new AddNewItemPanel<Long, Airport, AirportPage>("new-panel", new Airport(), AirportPage.class).setVisible(false));



		divTable.add(new OrderByBorder<SingularAttribute<Airport, ?>>("sortByName", Airport_.name, airportDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Airport, ?>>("sortByCode", Airport_.code, airportDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Airport, ?>>("sortByType", Airport_.type, airportDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Airport, ?>>("sortByRegion", Airport_.region, airportDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Airport, ?>>("sortByContinent", Airport_.continent, airportDataProvider));
		divTable.add(new OrderByBorder("sortByCountry", Region_.country, airportDataProvider));

		filterForm.add(divTable);
		add(pagingNavigator);
		add(filterForm);

	}

}
